#include "tree.h"
#include <iostream>
#include <queue>
using namespace std;

tree::tree(void)
{
	root = NULL;
}

tree::~tree(void)
{
	if(root)
		delete root;
	root = NULL;
}

Nptr tree::insertNode(Nptr T, dataType key)
{
	if(T == NULL){
		T = new NODE;
		T->data = key;
		T->left_child = NULL;
		T->right_child = NULL;
	}
	else if(T->data > key)
		T->left_child = insertNode(T->left_child, key);
	else
		T->right_child = insertNode(T->right_child, key);
	return T;
}

Nptr tree::searchTree(Nptr T, dataType key)
{
	if(T==NULL)
		return NULL;
	else if(T->data == key)
		return T;
	else if(T->data > key)
		return searchTree(T->left_child, key);
	else
		return searchTree(T->right_child, key);
}

Nptr tree::copyTree(Nptr T)
{
	if(T == NULL)
		return NULL;
	else{
		Nptr T1 = new NODE;
		T1->data = T->data;
		T1->left_child = copyTree(T->left_child);
		T1->right_child = copyTree(T->right_child);
		return T1;
	}
}

void tree::preOrder(Nptr T)
{
	if(T != NULL){
		cout << T->data << " ";
		preOrder(T->left_child);
		preOrder(T->right_child);
	}
}

void tree::inOrder(Nptr T)
{
	if(T != NULL){
		inOrder(T->left_child);
		cout << T->data << " ";
		inOrder(T->right_child);
	}
}

void tree::postOrder(Nptr T)
{
	if(T != NULL){
		postOrder(T->left_child);
		postOrder(T->right_child);
		cout << T->data << " ";
	}

}
